<!DOCTYPE html>
<html>
<head>
    <title>3D展示系统</title>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
        }

        .header {
            background-color: #333;
            color: white;
            padding: 1rem;
            height: 60px;
        }

        .container {
            display: flex;
            height: calc(100vh - 60px);
        }

        .sidebar {
            width: 250px;
            background-color: #f5f5f5;
            padding: 1rem;
            min-width: 250px;
        }

        .nav-menu {
            list-style: none;
            margin-top: 20px;
            width: 100%;
        }

        .nav-menu li {
            padding: 15px 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            border-radius: 5px;
            margin-bottom: 10px;
            background-color: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            width: 100%;
        }

        .nav-menu li:hover, .nav-menu li.selected {
            background-color: #333;
            color: white;
            transform: translateX(5px);
        }

        /* 在现有的 CSS 中添加或修改 */
        .nav-menu li.selected {
            background-color: #333;
            color: white;
            transform: translateX(5px);
}

        .content {
            flex: 1;
            padding: 1rem;
            background-color: #fff;
        }

        /* 轮播图样式 */
        .carousel {
            width: 100%;
            height: 400px;
            position: relative;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }

        .carousel img {
            position: absolute;
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: opacity 0.5s ease-in-out;
            opacity: 0;
        }

        .carousel-buttons {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            gap: 0.5rem;
            z-index: 10;
        }

        .carousel-button {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: rgba(255,255,255,0.5);
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .carousel-button:hover {
            transform: scale(1.2);
        }

        .carousel-button.active {
            background-color: white;
            transform: scale(1.2);
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>3D展示系统</h1>
    </div>

    <div class="container">
        <div class="sidebar">
            <ul class="nav-menu">
                <li onclick="location.href='/'">首页</li>
                <li onclick="location.href='/3d-demo'">3D手势控制演示</li>
            </ul>
        </div>

        <div class="content">
            <div class="carousel" id="mainCarousel"></div>
        </div>
    </div>

    <script>
        class Carousel {
            constructor(element, images) {
                this.element = element;
                this.images = images;
                this.currentIndex = 0;
                this.isTransitioning = false;
                this.touchStartX = 0;
                this.touchEndX = 0;
                this.init();
            }

            init() {
                // 创建图片容器
                this.imageContainer = document.createElement('div');
                this.imageContainer.style.position = 'relative';
                this.imageContainer.style.width = '100%';
                this.imageContainer.style.height = '100%';
                this.element.appendChild(this.imageContainer);

                // 创建所有图片元素
                this.imageElements = this.images.map((src, index) => {
                    const img = document.createElement('img');
                    img.src = src;
                    img.style.opacity = index === 0 ? '1' : '0';
                    img.draggable = false;
                    this.imageContainer.appendChild(img);
                    return img;
                });

                // 创建导航按钮
                const buttonsContainer = document.createElement('div');
                buttonsContainer.className = 'carousel-buttons';
                
                this.images.forEach((_, index) => {
                    const button = document.createElement('div');
                    button.className = `carousel-button ${index === 0 ? 'active' : ''}`;
                    button.addEventListener('click', () => this.showImage(index));
                    buttonsContainer.appendChild(button);
                });
                
                this.element.appendChild(buttonsContainer);

                // 初始化WebSocket连接
                this.initWebSocket();
            }

            initWebSocket() {
                const ws = new WebSocket('ws://localhost:8080/ws');
                
                // 获取所有菜单项并存储为数组
                const menuItems = Array.from(document.querySelectorAll('.nav-menu li'));
                const menuUrls = ['/', '/3d-demo'];  // 菜单对应的URL
                let currentMenuIndex = 0;  // 首页默认选中第一个菜单项
                let lastMenuUpdateTime = 0;
                let canConfirm = true;  // 添加确认控制标志
                
                // 初始化时高亮第一个菜单项
                menuItems[currentMenuIndex].classList.add('selected');
                
                ws.onmessage = (event) => {
                    const data = JSON.parse(event.data);
                    
                    if (data.status === 'MOVE' && data.hand_type === 'Right') {
                        // 右手控制轮播图
                        const x = data.x;
                        if (x > 65) {
                            this.next();
                        } else if (x < 35) {
                            this.prev();
                        }
                    } 
                    else if (data.hand_type === 'Left') {
                        const now = Date.now();
                        if (now - lastMenuUpdateTime > 1000) { // 1秒冷却时间
                            if (data.status === 'MENU') {
                                // 左手上下移动选择菜单
                                const y = data.y;
                                let newIndex = currentMenuIndex;
                                
                                if (y < 35) { // 手向上移动
                                    newIndex = Math.max(0, currentMenuIndex - 1);
                                } else if (y > 65) { // 手向下移动
                                    newIndex = Math.min(menuItems.length - 1, currentMenuIndex + 1);
                                }
                                
                                // 只有当索引发生变化时才更新
                                if (newIndex !== currentMenuIndex) {
                                    // 移除之前的选中状态
                                    menuItems[currentMenuIndex].classList.remove('selected');
                                    
                                    // 更新当前索引
                                    currentMenuIndex = newIndex;
                                    
                                    // 添加新的选中状态
                                    menuItems[currentMenuIndex].classList.add('selected');
                                    
                                    lastMenuUpdateTime = now;
                                    
                                    // 当选择了不同的页面时，启用确认功能
                                    const currentPath = window.location.pathname;
                                    const targetUrl = menuUrls[currentMenuIndex];
                                    canConfirm = currentPath !== targetUrl;
                                    
                                    console.log('当前选中菜单索引:', currentMenuIndex); // 调试输出
                                }
                            }
                            else if (data.status === 'CONFIRM' && canConfirm) {
                                // 只有当canConfirm为true且目标页面不是当前页面时才跳转
                                const targetUrl = menuUrls[currentMenuIndex];
                                if (targetUrl && targetUrl !== window.location.pathname) {
                                    console.log('跳转到:', targetUrl); // 调试输出
                                    window.location.href = targetUrl;
                                }
                                lastMenuUpdateTime = now;
                            }
                        }
                    }
                };
            }

            showImage(index) {
                if (this.isTransitioning || index === this.currentIndex) return;
                
                this.isTransitioning = true;
                
                // 更新按钮状态
                const buttons = this.element.querySelectorAll('.carousel-button');
                buttons[this.currentIndex].classList.remove('active');
                buttons[index].classList.add('active');
                
                // 切换图片
                this.imageElements[this.currentIndex].style.opacity = '0';
                this.imageElements[index].style.opacity = '1';
                
                this.currentIndex = index;
                
                setTimeout(() => {
                    this.isTransitioning = false;
                }, 500);
            }

            prev() {
                if (this.isTransitioning) return;
                const prevIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;
                this.showImage(prevIndex);
            }

            next() {
                if (this.isTransitioning) return;
                const nextIndex = (this.currentIndex + 1) % this.images.length;
                this.showImage(nextIndex);
            }
        }

        // 初始化轮播图
        const mainImages = [
            'https://img.freepik.com/free-vector/3d-crystal-style-shape-collection_23-2149850373.jpg',
            'https://img.freepik.com/free-vector/realistic-3d-shapes-floating_23-2148938128.jpg',
            'https://img.freepik.com/free-vector/gradient-3d-geometric-shapes-background_52683-65339.jpg',
            'https://img.freepik.com/free-vector/3d-wireframe-landscape-with-flowing-particles_1048-12631.jpg',
            'https://img.freepik.com/free-vector/3d-hand-gesture-technology-vector-digital-remix_53876-119824.jpg',
            'https://img.freepik.com/free-vector/3d-earth-graphic-symbolizing-global-trade-illustration_456031-125.jpg'
        ];

        // 等待 DOM 加载完成后初始化轮播图
        document.addEventListener('DOMContentLoaded', () => {
            new Carousel(document.getElementById('mainCarousel'), mainImages);
        });
    </script>
</body>
</html>
